<template>
    <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts 主题
import { debounce } from "utils";

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "96%"
    },
    height: {
      type: String,
      default: "400px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    dataAll: {
      type: Array
    }
  },
  data() {
    return {
      chart: null,
      time: [],
      firstOverRate: [],
      comprehensiveFirstRate: [],
      s1Arr: [],
      s2Arr: [],
      s3Arr: [],
      s4Arr: [],
      s5Arr: [],
      // 统计日
      amountDate: [],
      // 首逾率
      dayZeroRate: [],
      // 14日
      fourteenRate: [],
      // 逾期率
      overdueRate: [],
      // 7日
      sevenRate: [],
      // 30+日
      thirtyAfterRate: [],
      // 30日
      thirtyRate: [],
      // 21日
      twentyOneRate: []
    };
  },
  watch: {
    dataAll: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      }
    }
  },
  mounted() {
    this.initChart();
    this.__resizeHanlder = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener("resize", this.__resizeHanlder);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    resetData() {
      // 统计日
      this.amountDate = []
      // 首逾率
      this.dayZeroRate = []
      // 14日
      this.fourteenRate = []
      // 逾期率
      this.overdueRate = []
      // 7日
      this.sevenRate = []
      // 30+日
      this.thirtyAfterRate = []
      // 30日
      this.thirtyRate = []
      // 21日
      this.twentyOneRate = []
    },
    setOptions(listAll) {
      this.resetData()
      if (listAll.length > 0) {
        listAll.forEach(item => {
          this.amountDate.push(item.amountDate)
          this.dayZeroRate.push(item.dayZeroRate)
          this.fourteenRate.push(item.fourteenRate)
          this.overdueRate.push(item.overdueRate)
          this.sevenRate.push(item.sevenRate)
          this.thirtyAfterRate.push(item.thirtyAfterRate)
          this.thirtyRate.push(item.thirtyRate)
          this.twentyOneRate.push(item.twentyOneRate)
        });
      }
      this.chart.setOption({
        color: [
          "#f2ad86",
          "#8ecac8",
          "#88cbf5",
          "#93c7a3",
          "#f8ec80",
          "#7896f8",
          "#ec6a6a"
        ],
        title: {
          text: ""
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            lineStyle: {
              type: "dotted",
              color: "#ccc"
            }
          }
        },
        legend: {
          data: [
            this.$t("OverdueCustomerList.col6"),
            this.$t("overdueData.firstOvers"),
            this.$t("overdueData.firstOvers7"),
            this.$t("overdueData.firstOvers14"),
            this.$t("overdueData.firstOvers21"),
            this.$t("overdueData.firstOvers30"),
            this.$t("overdueData.firstOvers31")
          ]
        },
        grid: {
          bottom: "10%",
          top: 90,
          containLabel: true
        },
        xAxis: {
          type: "category",
          name: this.$t("overdueData.time"),
          nameTextStyle: {
            width: 50,
            fontSize: 16,
            padding: 10,
            align: "right"
          },
          boundaryGap: false,
          data: this.amountDate,
          axisLabel: {
            interval: 0,
            rotate: 40
          }
        },
        yAxis: {
          type: "value",
          name: this.$t("overdueData.firstOvers"),
          nameTextStyle: {
            fontSize: 16,
            padding: 10
          },
          splitLine: {
            show: false
          },
          splitArea: {
            show: false
          }
        },
        series: [
          {
            name: this.$t("OverdueCustomerList.col6"),
            type: "line",
            data: this.dayZeroRate
          },
          {
            name: this.$t("overdueData.firstOvers"),
            type: "line",
            data: this.overdueRate
          },
          {
            name: this.$t("overdueData.firstOvers7"),
            type: "line",
            data: this.sevenRate
          },
          {
            name: this.$t("overdueData.firstOvers14"),
            type: "line",
            data: this.fourteenRate
          },
          {
            name: this.$t("overdueData.firstOvers21"),
            type: "line",
            data: this.twentyOneRate
          },
          {
            name: this.$t("overdueData.firstOvers30"),
            type: "line",
            data: this.thirtyRate
          },
          {
            name: this.$t("overdueData.firstOvers31"),
            type: "line",
            data: this.thirtyAfterRate
          }
        ]
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.dataAll);
    }
  }
};
</script>
<style scoped lang='scss'>
.chart {
  min-width: 800px !important;
  overflow: auto;
}
</style>
